<?php

namespace app\admins\model;

use think\Model;

class Test extends Model
{
    protected $pk = 'id';
    protected $name = 'test';

    // 模型字段
    protected $schema = [
        'id' => 'int',
        'meta_key' => 'string',
        'meta_value' => 'string',
        'status' => 'int',
        'time' => 'int'
    ];

    // 验证常用规则
    private function validate_rules()
    {
        $rules = [
            'name'  => 'require|max:25',
            'age'   => 'number|between:1,120',
            'email' => 'email',
            'url' => 'url',
            'repassword' => 'require|confirm:password', // 字段是否和另外一个字段的值一致
            'name' => 'require|different:account', // 不一致
            'name'  => ['require', 'max' => 25, 'regex' => '/^[\w|\d]\w+/'],
            'ip' => 'ip',
            'ip' => 'filter:validate_ip', // filter_var 验证
            'num' => 'float', // integer, boolean
            'info' => 'array',
            'date' => 'date',
            'create_time' => 'dateFormat:y-m-d',
            'mobile' => 'mobile',
            'id_card' => 'idCard', // 身份证
            'mac' => 'macAddr',
            'zip' => 'zip', // 邮政编码

            // =:100 >=:100 <=:100 <:100
            'num' => '=:100',
            'price' => '<:market_price', // 字段比较
            // in:1,2,3 notIn:1,2,3 between:1,10 notBetween:1,10
            'num' => 'in:1,2,3', // 在范围内
            // length:4 length:4,25 max:25 min:5
            'name' => 'length:4,25', // 长度范围
            'begin_time' => 'after:2016-3-18',
            'end_time'   => 'before:2016-10-01',
            'expire_time'   => 'expire:2016-2-1,2016-10-01', // 期限
            // allow1,allow2,...
            'name'   => 'allowIp:114.45.4.55',
            'name'   => 'denyIp:114.45.4.55',
            'accepted' => ['regex' => '/^(yes|on|1)$/i'], // 正则

            // alphaNum, alphaDash, chs, chsAlpha, chsAlphaNum, chsDash
            // lower, upper, space, xdigit
            // file, image:width,height,type
            // fileExt:允许的文件后缀
            // fileMime:允许的文件类型
            // ileSize:允许的文件字节大小
            // token:表单令牌名称
            'name' => 'alpha',
            'host' => 'activeUrl', // 有效的域名或者IP
            // unique:table,field,except,pk name字段的值是否在user表
            // 'name' => 'unique:user,status=1&account='.$data['account']

            // 当account有值的时候password字段必须
            'password' => 'requireWith:account',
            // 当account的值等于1的时候 password必须
            'password' => 'requireIf:account,1',
            // mobile和phone必须输入一个
            'mobile' => 'requireWithout:phone',
            'phone'  => 'requireWithout:mobile',

            // 使用check_require方法检查是否需要验证age字段必须
            'age' => 'requireCallback:check_require|number'
            // 函数返回true的时候age字段是必须的
            // function check_require($value, $data){
            //     if(empty($data['birthday'])){
            //         return true;
            //     }
            // }
        ];
    }

    // 表单常用字段
    private function form_fields() {
        $fields = [
            ['label' => '输入框',  'name' => 'title', 'verify' => 'required'],
            ['label' => '密码框', 'name' => 'password', 'type' => 'password', 'desc' => '辅助文字'],
            ['label' => '选择框', 'name' => 'select', 'type' => 'select', 'value' => 'one', 'options' => ['one' => '选择一', 'two' => '选择二']],
            ['label' => '复选框', 'name' => 'checkbox', 'type' => 'checkbox', 'value' => ['one'], 'options' => ['one' => '复选一', 'two' => '复选二']],
            ['label' => '开启', 'name' => 'open', 'type' => 'checkbox', 'value' => '0', 'title' => '开启'],
            ['label' => '开关', 'name' => 'switch', 'type' => 'switch', 'value' => '0', 'lay-text' => 'ON|OFF'],
            ['label' => '单选框', 'name' => 'radio', 'type' => 'radio', 'value' => 'one', 'options' => ['one' => '单选一', 'two' => '单选二']],
            ['label' => '上传', 'name' => 'upload', 'type' => 'upload'],
            ['label' => '颜色', 'name' => 'color', 'type' => 'color'],
            ['label' => '日期', 'name' => 'date', 'type' => 'date', 'data-type' => 'date'], // year month time datetime
            ['label' => '文本域', 'name' => 'textarea', 'type' => 'textarea', 'rows' => 5],
            ['label' => '富文本', 'name' => 'editor', 'type' => 'editor'], // layui 内置富文本
            ['label' => 'wangeditor', 'name' => 'weditor', 'type' => 'weditor'], // wangeditor 富文本
        ];
    }
}
